package com.roy.galaxy.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-28 09:43
 */
public class 字符串转整数 {

    List<Integer> ints = new ArrayList(Arrays.asList(0,1,2,3,4,5,6,7,8,9));

    List<String> flags = new ArrayList(Arrays.asList("-"));
    public int myAtoi(String str) {

        if(str.length() == 0){
            return 0;
        }

        if(str.equals(" ")){
           return 0;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0 ; i < str.length() ; i ++ ){

            if(str.charAt(i) == 0){
                stringBuffer.append(str.charAt(i));

            }else if(!ints.contains(str.charAt(i))){
               break;
            }
            stringBuffer.append(str.charAt(i));
        }
        if(Integer.valueOf(stringBuffer.toString())  > Integer.MAX_VALUE ){
            return Integer.MAX_VALUE;
        }

        if(Integer.valueOf(stringBuffer.toString())  > Integer.MAX_VALUE){
            return Integer.MIN_VALUE;
        }

        return Integer.valueOf(stringBuffer.toString());

    }


    public static void main(String[] args) {
        System.out.println(new 字符串转整数().myAtoi(" "));
    }
}
